﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace CodeStandardAddin.View
{
	public class CheckBoxGroup : GroupBox
	{
		#region Fields --------------------------------------------------------------------------------

		private CheckBox _cbxTitle = new CheckBox();

		#endregion Fields -----------------------------------------------------------------------------


		#region Constructors --------------------------------------------------------------------------

		public CheckBoxGroup()
		{
			_cbxTitle.AutoSize = true;
			_cbxTitle.BackColor = this.BackColor;
			_cbxTitle.Checked = true;
			_cbxTitle.Location = new Point(3, 0);
			_cbxTitle.CheckedChanged += new EventHandler(CbxTitle_CheckedChanged);

			this.Controls.Add(_cbxTitle);
		}

		#endregion Constructors -----------------------------------------------------------------------


		#region Properties ----------------------------------------------------------------------------

		[DefaultValue(true)]
		public bool Checked
		{
			get { return _cbxTitle.Checked; }
			set { _cbxTitle.Checked = value; }
		}

		[Bindable(true)]
		public override string Text
		{
			get { return _cbxTitle.Text; }
			set { _cbxTitle.Text = value; }
		}

		#endregion Properties -------------------------------------------------------------------------


		#region Functions -----------------------------------------------------------------------------

		protected override void OnControlAdded(ControlEventArgs e)
		{
			base.OnControlAdded(e);
			e.Control.Enabled = _cbxTitle.Checked;
		}

		protected override void OnBackColorChanged(EventArgs e)
		{
			base.OnBackColorChanged(e);
			_cbxTitle.BackColor = this.BackColor;
		}

		protected override void OnEnabledChanged(EventArgs e)
		{
			base.OnEnabledChanged(e);
			foreach (System.Windows.Forms.Control control in Controls)
			{
				control.Enabled = (control == _cbxTitle ? this.Enabled : _cbxTitle.Checked);
			}
		}

		private void CbxTitle_CheckedChanged(object sender, EventArgs e)
		{
			foreach (System.Windows.Forms.Control control in this.Controls)
			{
				if (control != _cbxTitle)
				{
					control.Enabled = _cbxTitle.Checked;
				}
			}
		}

		#endregion Functions --------------------------------------------------------------------------
	}
}